<?php
global $_W;
set_time_limit(0);

$mems = getall("select id,openid,unionid from ims_junsion_listenbook_mem where uniacid='{$_W['uniacid']}' and unionid in (select unionid from ims_junsion_listenbook_mem group by unionid having count(unionid) > 1) and unionid!='' order by unionid");
foreach ($mems as $value) {
	$ms[$value['unionid']][] = $value;
}
foreach ($ms as $unionid => $arr) {
	$tmem = $arr[0];
	$nmem = $arr[1];
	update('mem',array('openid'=>$nmem['openid']),array('id'=>$tmem['id']));
	del('mem',array('id'=>$nmem['id']));
	if (count($arr) > 2){
		unset($arr[0]);
		unset($arr[1]);
		foreach ($arr as $value) {
			del('mem',array('id'=>$value['id']));
		}
	}
}
exit;

$mems = getall("select id,nickname from ".tb('mem')." where uniacid='{$_W['uniacid']}' and unionid=''"); 
$num = 0;
foreach ($mems as $value) {
	$m = get("select id,openid,unionid from ".tb('mem')." where uniacid='{$_W['uniacid']}' and nickname='{$value['nickname']}' and unionid!=''");
	if ($m['id']){
		update('mem',array('openid'=>$m['openid'],'unionid'=>$m['unionid']),array('id'=>$value['id']));
		del('mem',array('id'=>$m['id']));
		$num ++;
	}
}



message('执行完成，有效执行'.$num."条记录");